package top.zhouze.common.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;
import top.zhouze.common.util.ReflectUtil;

import java.lang.reflect.ParameterizedType;
import java.util.Objects;

/**
 * @author 周仄
 * @since 2023/6/26 9:01
 */
public abstract class BaseActivity<V extends ViewDataBinding, VM extends BaseViewModel> extends ViewBindingActivity<V> {
    protected VM viewModel;


    @SuppressLint("MissingSuperCall")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        callSuper(savedInstanceState);
        init();
        //页面接受的参数方法
        initParam();
        //页面数据初始化方法
        initData();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initViewObservable();
    }

    @Override
    void init() {
        super.init();
        initViewModel();
        viewModel.getFinishEvent().observe(this, isFinish -> {
            if (isFinish) {
                finish();
            }
        });
        viewModel.getToastEvent().observe(this, this::showToast);
    }

    private void initViewModel() {
        Class<VM> vmCls = ReflectUtil.getGenericClass(this,1);
        viewModel = new ViewModelProvider(this).get(vmCls);
        viewModel.setLifecycleOwner(this);
    }

}
